package com.online.fund;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;

import org.mybatis.spring.annotation.MapperScan;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;

import com.online.fund.spider.FundAssetAllocationPageProcesser;
import com.online.fund.spider.FundPageProcesser;
import com.online.fund.spider.FundSharesProcesser;
import com.online.fund.spider.IndustryPlateProcesser;
import com.online.fund.spider.SharesAllTransactionProcesser;
import com.online.fund.spider.SharesDailyTransactionProcesser;
import com.online.fund.spider.SharesIndustryPageProcess;
import com.online.fund.spider.SharesRefIdPageProcesser;
import com.online.fund.util.HttpClientUtil;

import jdk.nashorn.api.scripting.ScriptObjectMirror;

@SpringBootApplication
@SuppressWarnings("restriction")
@EnableCaching
@MapperScan("com.online.fund.spider.dao")
public class FundApplication {
	
	private final static Logger logger = LoggerFactory.getLogger(FundApplication.class);

	public static void main(String[] args) {
		ConfigurableApplicationContext context = SpringApplication.run(FundApplication.class, args);
		
		// 基金数据抓取
//		processFund(context);
		
		// 当天股票交易数据
//		processSharesTransaction(context);
//		processAllSharesTransaction(context);
//		processSharesIndustry(context);
		
		// 当天行业板块数据抓取
//		processIndustryPlate(context);
		
		// 获取股票相关信息
//		processSharesRefIdInfo(context);
	}
	
	public static void processSharesRefIdInfo(ConfigurableApplicationContext context) {
		SharesRefIdPageProcesser processer = context.getBean(SharesRefIdPageProcesser.class);
		processer.parsePage();
	}

	// 当天行业板块数据抓取
	public static void processIndustryPlate(ConfigurableApplicationContext context) {
		IndustryPlateProcesser processer = context.getBean(IndustryPlateProcesser.class);
		processer.parsePage();
	}

	// 获取股票行业信息
	public static void processSharesIndustry(ConfigurableApplicationContext context) {
		SharesIndustryPageProcess sharesIndustryPageProcess = context.getBean(SharesIndustryPageProcess.class);
		sharesIndustryPageProcess.parsePage();
	}

	// 当天股票交易数据
	public static void processSharesTransaction(ApplicationContext context) {
		SharesDailyTransactionProcesser sharesTransactionProcesser = context.getBean(SharesDailyTransactionProcesser.class);
		sharesTransactionProcesser.parsePage();
	}
	
	// 股票历史交易数据
	public static void processAllSharesTransaction(ApplicationContext context) {
		SharesAllTransactionProcesser sharesTransactionProcesser = context.getBean(SharesAllTransactionProcesser.class);
		sharesTransactionProcesser.parsePage();
	}

	/**
	 * 开放式基金净值
	 */
	public static void processFund(ApplicationContext context) {
		try {
			FundPageProcesser fundPageProcesser = context.getBean(FundPageProcesser.class); 
			FundSharesProcesser fundSharesProcesser = context.getBean(FundSharesProcesser.class); 
			FundAssetAllocationPageProcesser fundAssetAllocationPageProcesser = context.getBean(FundAssetAllocationPageProcesser.class);
			
			
//			List<String> list = new ArrayList<String>();
//			list.add("002632");
//			
//			for (String code : list) {
//				new Thread(() -> {
//					try {
//						fundPageProcesser.parsePage(code);
//					} catch (Exception e) {
//						logger.error("---基金" + code, e);
//					}
//				}).start(); ;
//				
//				new Thread(() -> {
//					try {
//						fundSharesProcesser.parsePage(code, "鑫元双债增强债券A");
//					} catch (Exception e) {
//						logger.error("---股票" + code, e);
//					}
//				}).start();
//				
//				new Thread(() -> {
//					try {
//						fundAssetAllocationPageProcesser.parsePage(code, "鑫元双债增强债券A");
//					} catch (Exception e) {
//						logger.error("---基金资产配置" + code, e);
//					}
//				}).start();
//			}
			
			String resultStr = HttpClientUtil.httpGetRequest(
					"http://fund.eastmoney.com/Data/Fund_JJJZ_Data.aspx?t=1&lx=1&letter=&gsid=&text=&sort=zdf,desc&page=1,6000&dt=1516200330479&atfc=&onlySale=0");
			
			ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
			ScriptEngine nashorn = scriptEngineManager.getEngineByName("nashorn");
			
			nashorn.eval(resultStr);
			ScriptObjectMirror db = (ScriptObjectMirror) nashorn.get("db");
			ScriptObjectMirror datas = (ScriptObjectMirror) db.get("datas");
			if (datas.isArray()) {
				for (int i = 0; i < datas.size(); i++) {
					ScriptObjectMirror data = (ScriptObjectMirror)datas.get("" + i);
					if (data.isArray()) {
						String fundCode = (String)data.get("0");
						String fundName = (String)data.get("1");
						
						System.out.println(fundCode + "   " + i);
						new Thread(() -> {
							try {
								fundPageProcesser.parsePage(fundCode);
							} catch (Exception e) {
								logger.error("---基金" + fundCode, e);
							}
						}).start();
						
						new Thread(() -> {
							try {
								fundSharesProcesser.parsePage(fundCode, fundName);
							} catch (Exception e) {
								logger.error("---基金持有股票" + fundCode, e);
							}
						}).start();
						
						new Thread(() -> {
							try {
								fundAssetAllocationPageProcesser.parsePage(fundCode, fundName);
							} catch (Exception e) {
								logger.error("---基金资产配置" + fundCode, e);
							}
						}).start();
						
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
